/*
 * Copyright 2004-2006 asoft
 * 本软件及其所有拷贝的名称，与光盘上或本软件中注明的公司同在。
 * 本软件及文档享有版权，并受国家版权法及国际协约条款的保护。
 * 您不可以从本软件中去掉其版权声明；并保证为本软件的拷贝（本部或部分）复制版权声明。
 * 您同意制止以任何形式非法拷贝本软件及文档。
 * 版权信息参见：
 *      http://www.asoft.cn/licenses
 */

package com.asoft.common.viewframe.dao;

import java.util.List;

import org.apache.log4j.Logger;

import com.asoft.common.base.dao.BaseDAOHibernate;
import com.asoft.common.viewframe.define.ColumnType;
import com.asoft.common.viewframe.model.Column;
import com.asoft.common.viewframe.util.AsoftViewFrameException;

/**
 * <p>Title: 栏目</p>
 * <p>Description: 栏目 Column </p>
 * <p>Copyright: Copyright (c) 2004-2006</p>
 * <p>Company: asoft</p>
 * @ $Author: amon.lei $
 * @ $Date: 2007-2-20 $
 * @ $Revision: 1.0 $
 * @ created in 2007-2-20
 *
 */

public class ColumnDAOimp extends BaseDAOHibernate
                          implements ColumnDAO
{
        static Logger logger = Logger.getLogger(ColumnDAOimp.class);

        /**
         * 描述:栏目
         *
         * @ param:
         * @ param:orderBy 排序属性（for hql）
         * @ param:sortType 排序方式（asc or desc）
         * @ Exception:
         * @ return model集合
         */
        public List findColumns(String homeId,
                                String areaId,
                                String superColumnId,
                                String columnId,
                                String code,
                                String name,
                                int type,
                                String orderBy,
                                String sortType)

        {
                if(type == ColumnType.ONE_LEVEL)
                {
                        String hql = 
                        " from Column column " + 
                        " where( " +
                        "     column.area.home.id like ? " +
                        " and column.area.id like ? " +
                        " and column.id like ? " +
                        " and column.code like ? " + 
                        " and column.name like ? " + 
                        " and column.type = ? " + 
                        " ) " + 
                         " order by " + orderBy + " " + sortType;
                         Object[] values = new Object[6];
                         values[0] = homeId;
                         values[1] = areaId;
                         values[2] = columnId;
                         values[3] = "%" + code + "%";
                         values[4] = "%" + name + "%";
                         values[5] = type;
                         return this.getHibernateTemplate().find(hql,values);
                }
                if(type == ColumnType.TWO_LEVEL)
                {
                        String hql = 
                        " from Column column " + 
                        " where( " +
                        "     column.superColumn.area.home.id like ? " +
                        " and column.superColumn.area.id like ? " +
                        " and column.superColumn.id like ? " +
                        " and column.id like ? " +
                        " and column.code like ? " + 
                        " and column.name like ? " + 
                        " and column.type = ? " + 
                        " ) " + 
                        " order by " + orderBy + " " + sortType;
                        Object[] values = new Object[7];
                        values[0] = homeId;
                        values[1] = areaId;
                        values[2] = superColumnId;
                        values[3] = columnId;
                        values[4] = "%" + code + "%";
                        values[5] = "%" + name + "%";
                        values[6] = type;
                        return this.getHibernateTemplate().find(hql,values);
                }
                throw new AsoftViewFrameException("未知栏目类型:" + type);
               
        }
        
        public Column getByCode(String code)
        {
                String hql = 
                        " from Column " +
                        " where( code = ? ) ";
                Object[] values = new Object[1];
                values[0] = code;
                List columns =  this.getHibernateTemplate().find(hql,values);
                if(columns.size() == 0)
                {
                        return null;
                }
                if(columns.size() > 1)
                {
                        throw new AsoftViewFrameException("代码为" + code + "的栏目有" + columns.size() + "个");
                }
                return (Column)columns.get(0);
        }

}
